from selenium.webdriver.common.by import By
import DBUtils
import Register


class TestResigister:

    def test_register1(self):
        # name,password,password

        driver = Register.register2('wangwu','123456','123456')

        sql = 'select * from user where username = %s'
        params = ['wangwu']
        data = DBUtils.select(sql,params)

        if len(data) == 1:
            print('通过')
        else:
            print('不通过')
            driver.save_screenshot(filename='wangwu-123456-123456.png')

        assert len(data) == 1

        driver.quit()

        sql = "delete from user where username = %s"
        params = ['wangwu']

        DBUtils.update(sql,params)


    def test_register2(self):

        sql = 'insert into user(username,password) values(%s,md5(%s))'
        params = ['wangwu1','123456']
        DBUtils.update(sql,params)
        # 先在数据库中插user表中插入一条username wangwu1 password 123456 的信息

        driver = Register.register2('wangwu1','123456','123456')

        sql = 'select * from user where username = %s'
        params = ['wangwu1']
        data = DBUtils.select(sql,params)

        if len(data) == 1:
            print('通过！')
        else:
            print('不通过')
            driver.save_screenshot('test_register2.png')

        assert len(data) == 1

        driver.quit()

        sql = "delete from user where username = %s"
        params = ['wangwu1']

        DBUtils.update(sql, params)


    def test_register3(self):

        driver = Register.register2('wangwu','123456','1234567')

        result = ''
        try:
            result = driver.find_element(By.XPATH,"//div[@class='login-center1']/form/div[3]/span[1]").text

        except Exception as e:
            print(e)

        if result == '两次密码不相同':
            print('通过')
        else:
            print('不通过')
            driver.save_screenshot('test_register3.png')

        driver.quit()

        assert result == '两次密码不相同'
        sql = "delete from user where username = %s"
        params = ['wangwu']
        DBUtils.update(sql, params)



    def test_register4(self):

        driver = Register.register2('wangwu','123456','')

        result = driver.find_element(By.XPATH,"//div[@class='login-center1']/form/div[3]/span[1]").text

        if result == '重复密码为空':
            print('通过')
        else:
            print('不通过')
            driver.save_screenshot('test_register4.png')

        driver.quit()

        assert result == '重复密码为空'



    def test_register5(self):

        driver = Register.register2('wangwu','','')

        result = driver.find_element(By.XPATH,"//div[@class='login-center1']/form/div[2]/span[1]").text

        if result == '注册密码不能为空':
            print('通过')
        else:
            print('不通过')
            driver.save_screenshot('test_register5.png')

        driver.quit()

        assert result == '注册密码不能为空'